package soccer.view;

import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextPane;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.border.CompoundBorder;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import java.awt.Color;
import java.awt.event.ActionListener;

import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import java.awt.SystemColor;
import javax.swing.border.EtchedBorder;

public class TeamSearchPanel extends JPanel {
	private final ButtonGroup buttonGroup = new ButtonGroup();
	
	private JTextField searchTextField;
	private JRadioButton searchByCityRadioButton;
	private JRadioButton searchByNameRadioButton;
    private JLabel searchTeamLabel;
    private JButton searchTeamButton;
    private JTable teamsTable;
    private JLabel searchInfoLabel;
    private JScrollPane teamTableScrollPane;
	/**
	 * Create the panel.
	 */
	public TeamSearchPanel() {
		setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Teams", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		setLayout(null);
		
		searchByCityRadioButton = new JRadioButton("By City");
		buttonGroup.add(searchByCityRadioButton);
		searchByCityRadioButton.setBounds(211, 65, 77, 23);
		searchByCityRadioButton.setActionCommand("searchCity");
		add(searchByCityRadioButton);
		
	    searchByNameRadioButton = new JRadioButton("By Name");
		buttonGroup.add(searchByNameRadioButton);
		searchByNameRadioButton.setBounds(113, 65, 77, 23);
		searchByNameRadioButton.setActionCommand("searchName");
		searchByNameRadioButton.setSelected(true);
		add(searchByNameRadioButton);
		
	    searchTeamLabel = new JLabel("Team Name :");
		searchTeamLabel.setBounds(113, 103, 77, 23);
		add(searchTeamLabel);
		
		searchTextField = new JTextField();
		searchTextField.setBounds(187, 103, 128, 23);
		add(searchTextField);
		searchTextField.setColumns(10);
		
	    searchTeamButton = new JButton("Search");
		searchTeamButton.setBounds(187, 142, 128, 23);
		add(searchTeamButton);
		
		teamTableScrollPane = new JScrollPane();
		teamTableScrollPane.setViewportBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		teamTableScrollPane.setBounds(45, 176, 378, 72);
		teamTableScrollPane.setVisible(false);
		add(teamTableScrollPane);
		
		teamsTable = new JTable();
		teamsTable.setCellSelectionEnabled(true);
		teamsTable.setColumnSelectionAllowed(true);
		teamsTable.setEnabled(false);
		teamTableScrollPane.setViewportView(teamsTable);
		teamsTable.setSurrendersFocusOnKeystroke(true);
		teamsTable.setToolTipText("");
		teamsTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Team Code", "Team Name", "Establish Date", "City"
			}
		));
		teamsTable.getColumnModel().getColumn(0).setMinWidth(25);
		teamsTable.getColumnModel().getColumn(2).setPreferredWidth(89);
		teamsTable.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
	
		
	    searchInfoLabel = new JLabel();
	    searchInfoLabel.setFont(new Font("Times New Roman", Font.PLAIN, 14));
	    searchInfoLabel.setForeground(new Color(153, 0, 0));
	    searchInfoLabel.setHorizontalAlignment(SwingConstants.CENTER);
		searchInfoLabel.setBounds(88, 176, 286, 23);
		add(searchInfoLabel);
	
	}

	public void addTeamsToTeamsTable(List<Map> teams){
		DefaultTableModel tableModel = (DefaultTableModel) teamsTable.getModel();
        resetTeamTable(tableModel);
		for(int i = 0; i < teams.size(); i++){
			Vector<Comparable> v = new Vector<Comparable>();
			Map team = teams.get(i);
			v.add((Comparable) team.get("teamKodu"));
			v.add((Comparable) team.get("teamName"));
			v.add((Comparable) team.get("establishDate"));
			v.add((Comparable) team.get("city"));
			tableModel.addRow(v);
			
			
		}
     searchInfoLabel.setVisible(false);
     teamTableScrollPane.setVisible(true);
     
	}
	public void showErrorMessage(String message){
		teamTableScrollPane.setVisible(false);
		searchInfoLabel.setText(message);
		searchInfoLabel.setVisible(true);
		
	}
	public void resetTeamTable(DefaultTableModel model){
		while(model.getRowCount() > 0){
			model.removeRow(0);
		}
	}
	public void addSearchTeamButtonActionListener(ActionListener l){
		searchTeamButton.addActionListener(l);
	}
	public void addSearchByNameRadioButtonActionListener(ActionListener l){
		searchByNameRadioButton.addActionListener(l);
	}
	public void addSearchByCityRadioButtonActionListener(ActionListener l){
		searchByCityRadioButton.addActionListener(l);
	}
	
	public JTextField getSearchTextField() {
		return searchTextField;
	}
	public void setSearchTextField(JTextField searchTextField) {
		this.searchTextField = searchTextField;
	}
	public JRadioButton getSearchByCityRadioButton() {
		return searchByCityRadioButton;
	}
	public void setSearchByCityRadioButton(JRadioButton searchByCityRadioButton) {
		this.searchByCityRadioButton = searchByCityRadioButton;
	}
	public JRadioButton getSearchByNameRadioButton() {
		return searchByNameRadioButton;
	}
	public void setSearchByNameRadioButton(JRadioButton searchByNameRadioButton) {
		this.searchByNameRadioButton = searchByNameRadioButton;
	}
	public JLabel getSearchTeamLabel() {
		return searchTeamLabel;
	}
	public void setSearchTeamLabel(JLabel searchTeamLabel) {
		this.searchTeamLabel = searchTeamLabel;
	}
	public JButton getSearchTeamButton() {
		return searchTeamButton;
	}
	public void setSearchTeamButton(JButton searchTeamButton) {
		this.searchTeamButton = searchTeamButton;
	}
	public JTable getTeamsTable() {
		return teamsTable;
	}
	public void setTeamsTable(JTable teamsTable) {
		this.teamsTable = teamsTable;
	}
	public ButtonGroup getButtonGroup() {
		return buttonGroup;
	}

	public void setSearchInfoLabel(JLabel searchInfoLabel) {
		this.searchInfoLabel = searchInfoLabel;
	}

	public JLabel getSearchInfoLabel() {
		return searchInfoLabel;
	}
}
 